package com.cindata

class Menu
{
    String title
    String url
    String activeUrl
    String icon

    Menu parent
    Integer orderNum
    Integer status
    //1:启用 2：禁用
    Date createdDate = new Date()
    Date modifiedDate = new Date()
    User createBy
    User modifyBy
    static hasMany = [children: Menu]
    static belongsTo = [parent: Menu]

    static transients = ['allChildren']

    def getAllChildren()
    {
        children ? children + children*.allChildren.flatten() : []
    }

    def shouldActive(String targetUrl)
    {
        if (targetUrl.indexOf(url) > -1)
        {
            return 'active'
        }
        children.each { cmenu ->
            if (targetUrl.indexOf(cmenu.url) > -1)
            {
                return 'active'
            }
        }
        return ''
    }
    def springSecurityService

    def beforeInsert()
    {
        def username = springSecurityService?.getPrincipal()?.username
        createBy = User.findByUsername(username)
    }

    def beforeUpdate()
    {
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }
    static constraints = {
        title blank: false
        url blank: true, nullable: true
        activeUrl blank: true, nullable: true, comment: '菜单激活url'
        parent blank: true, nullable: true
        orderNum blank: true, nullable: true
        status blank: true, nullable: true
        createBy blank: true, nullable: true
        createdDate blank: true, nullable: true
        modifyBy blank: true, nullable: true
        modifiedDate blank: true, nullable: true

        icon blank: true, nullable: true
    }
    static mapping = {
        sort orderNum: 'asc'
        parent lazy: false
    }

    String toString()
    {
        if (parent?.title)
        {
            "${title}(${parent?.title})"
        }
        else
        {
            "${title}"
        }
    }

    def setValues(map)
    {
        map.each { key, value -> this."${key}" = value
        }
    }

    /*
    * 初始化菜单数据
    */

    static initData()
    {
        def menu, cmenu
        def orderNum = 0
        def user = User.findByUsername('admin')

        // 首页
        menu = Menu.findByTitle('首页')
        if (!menu)
        {
            menu = new Menu()
        }
        menu.properties = [title: '首页', url: '/main/index', activeUrl: '/main/', status: 1, createBy: user, parent: null, orderNum: ++orderNum]
        menu.save()

        // 押品
        menu = Menu.findByTitle('押品')
        if (!menu)
        {
            menu = new Menu()
        }
        menu.properties = [title: '押品', url: '/estate/index', activeUrl: '/estate/', status: 1, createBy: user, parent: null, orderNum: ++orderNum]
        menu.save()

        // 贷款
        menu = Menu.findByTitle('贷款')
        if (!menu)
        {
            menu = new Menu()
        }
        menu.properties = [title: '贷款', url: '/mortgage/index', activeUrl: '/mortgage/', status: 1, createBy: user, parent: null, orderNum: ++orderNum]
        menu.save()

        // 借款人
        menu = Menu.findByTitle('借款人')
        if (!menu)
        {
            menu = new Menu()
        }
        menu.properties = [title: '借款人', url: '#', activeUrl: '/contact/', status: 1, createBy: user, parent: null, orderNum: ++orderNum]
        menu.save()

        cmenu = Menu.findByTitle('对私客户')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '对私客户', url: '/contact/index', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        cmenu = Menu.findByTitle('对公机构')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '对公机构', url: '/account/index', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        // 评估记录
        menu = Menu.findByTitle('评估记录')
        if (!menu)
        {
            menu = new Menu()
        }
        menu.properties = [title: '评估记录', url: '/valuationHistory/index', activeUrl: '/valuationHistory/', status: 1, createBy: user, parent: null, orderNum: ++orderNum]
        menu.save()

        // 导入
        menu = Menu.findByTitle('导入')
        if (!menu)
        {
            menu = new Menu()
        }
        menu.properties = [title: '导入', url: '/estateImportBatch/index', activeUrl: '/estateImportBatch/', status: 1, createBy: user, parent: null, orderNum: ++orderNum]
        menu.save()

        // 分析
        menu = Menu.findByTitle('分析')
        if (!menu)
        {
            menu = new Menu()
        }
        menu.properties = [title: '分析', url: '#', activeUrl: '/analyze/', status: 1, createBy: user, parent: null, orderNum: ++orderNum]
        menu.save()

        cmenu = Menu.findByTitle('分析统计')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '分析统计', url: '/analyze/index', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        cmenu = Menu.findByTitle('分析报告')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '分析报告', url: '/analyze/report', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        cmenu = Menu.findByTitle('全量押品分析报告')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '全量押品分析报告', url: '/analyze/fullQuantityReport', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        // 地图
        menu = Menu.findByTitle('地图')
        if (!menu)
        {
            menu = new Menu()
        }
        menu.properties = [title: '地图', url: '#', activeUrl: '/map/', status: 1, createBy: user, parent: null, orderNum: ++orderNum]
        menu.save()

        cmenu = Menu.findByTitle('热力图')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '热力图', url: '/newMap/heatmap', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        cmenu = Menu.findByTitle('聚合图')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '聚合图', url: '/newMap/cluster', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        cmenu = Menu.findByTitle('散点图')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '散点图', url: '/newMap/aggregation', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        cmenu = Menu.findByTitle('周边网点')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '周边网点', url: '/newMap/website', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        // 楼盘评分
        menu = Menu.findByTitle('楼盘评分')
        if (!menu)
        {
            menu = new Menu()
        }
        menu.properties = [title: '楼盘评分', url: '/buildingsRated/index', activeUrl: '/buildingsRated/', status: 1, createBy: user, parent: null, orderNum: ++orderNum]
        menu.save()

        // 开发商查询
        menu = Menu.findByTitle('开发商查询')
        if (!menu)
        {
            menu = new Menu()
        }
        menu.properties = [title: '开发商查询', url: '/developer/search', status: 1, createBy: user, parent: null, orderNum: ++orderNum]
        menu.save()

        // 评估
        menu = Menu.findByTitle('评估')
        if (!menu)
        {
            menu = new Menu()
        }
        menu.properties = [title: '评估', url: '#', activeUrl: '/assess/', status: 1, createBy: user, parent: null, orderNum: ++orderNum]
        menu.save()

        cmenu = Menu.findByTitle('房屋评估')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '房屋评估', url: '/assess/assess', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        cmenu = Menu.findByTitle('评估管理')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '评估管理', url: '/assess/history', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        // 系统管理
        menu = Menu.findByTitle('系统管理')
        if (!menu)
        {
            menu = new Menu()
        }
        menu.properties = [title: '系统管理', url: '#', activeUrl: '/user/', status: 1, createBy: user, parent: null, orderNum: ++orderNum]
        menu.save()

        cmenu = Menu.findByTitle('用户')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '用户', url: '/user/index', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        cmenu = Menu.findByTitle('登录明细')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '登录明细', url: '/loginHistory/index', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        cmenu = Menu.findByTitle('机构设置')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '机构设置', url: '/account/accountIndex', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        cmenu = Menu.findByTitle('角色权限')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '角色权限', url: '/roleMenu/index', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        cmenu = Menu.findByTitle('数据迁移')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '数据迁移', url: '/dataMigration/index', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        cmenu = Menu.findByTitle('地图绘制')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '地图绘制', url: '/precinct/index', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        cmenu = Menu.findByTitle('数据字典')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '数据字典', url: '/customerDimension/index', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        cmenu = Menu.findByTitle('费用统计')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '费用统计', url: '/userCost/userCostStatistics', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()

        cmenu = Menu.findByTitle('楼盘打分配置')
        if (!cmenu)
        {
            cmenu = new Menu()
        }
        cmenu.properties = [title: '楼盘打分配置', url: '/communityScoreWeight/index', status: 1, createBy: user, parent: menu, orderNum: ++orderNum]
        cmenu.save()
    }
}
